/*
 * Powered By dico
 * Since 2017 - 2018
 */
package com.seeyoui.kensite.bussiness.orderlang.productUnit.controller;


import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.seeyoui.kensite.common.util.DateUtils;
import com.seeyoui.kensite.common.constants.StringConstant;
import com.seeyoui.kensite.common.base.domain.EasyUIDataGrid;
import com.seeyoui.kensite.common.base.controller.BaseController;
import com.seeyoui.kensite.common.util.RequestResponseUtil;
import com.seeyoui.kensite.common.util.Result;
import com.seeyoui.kensite.common.util.excel.ExportExcel;

import com.seeyoui.kensite.common.base.domain.Page;


import com.alibaba.fastjson.JSONPath;
import com.seeyoui.kensite.bussiness.orderlang.productUnit.domain.ProductUnit;
import com.seeyoui.kensite.bussiness.orderlang.productUnit.service.ProductUnitService;

/**
 * ProductUnit
 * 
 * @author dico
 * @version 2.0
 * @since 1.0
 * @date 2018-02-07
 */

@Controller
@RequestMapping(value = "productUnit")
public class ProductUnitController extends BaseController {

	@Autowired
	private ProductUnitService productUnitService;

	/**
	 * 展示列表页面
	 * 
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	// @RequiresPermissions("productUnit:productUnit:view")
	@RequestMapping(value = "/{page}")
	public ModelAndView view(HttpSession session, HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, @PathVariable String page) throws Exception {
		return new ModelAndView("bussiness/orderlang/productUnit/" + page, modelMap);
	}

	/**
	 * 根据ID查询单条数据
	 * 
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	// @RequiresPermissions("productUnit:productUnit:select")
	@RequestMapping(value = "/data/{id}")
	@ResponseBody
	public Object data(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap,
			@PathVariable String id) throws Exception {
		ProductUnit productUnit = productUnitService.findOne(id);
		return productUnit;
	}

	/**
	 * 根据ID查询单条数据并返回相应表单
	 * 
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	// @RequiresPermissions("productUnit:productUnit:view")
	@RequestMapping(value = "/form/{page}/{id}")
	public ModelAndView form(HttpSession session, HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, @PathVariable String page, @PathVariable String id) throws Exception {
		ProductUnit productUnit = productUnitService.findOne(id);
		modelMap.put("productUnit", productUnit);
		return new ModelAndView("bussiness/orderlang/productUnit/" + page, modelMap);
	}

	/**
	 * 获取列表展示数据
	 * 
	 * @param modelMap
	 * @param productUnit
	 * @return
	 * @throws Exception
	 */
	// @RequiresPermissions("productUnit:productUnit:select")
	@RequestMapping(value = "/list/data", method = RequestMethod.POST)
	@ResponseBody
	public Object listData(HttpSession session, HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductUnit productUnit) throws Exception {
	    productUnit.setDelFlag("0");
		List<ProductUnit> productUnitList = productUnitService.findList(productUnit);
		int total = productUnitService.findTotal(productUnit);
		EasyUIDataGrid eudg = new EasyUIDataGrid();
		eudg.setTotal(String.valueOf(total));
		eudg.setRows(productUnitList);
		return eudg;
	}

	/**
	 * 获取列表展示数据带分页器用于jsp自己做分页
	 * 
	 * @param modelMap
	 * @param productUnit
	 * @return
	 * @throws Exception
	 */
	// @RequiresPermissions("productUnit:productUnit:view")
	@RequestMapping(value = "/list/page", method = RequestMethod.POST)
	@ResponseBody
	public Object listPage(HttpSession session, HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductUnit productUnit) throws Exception {
	    productUnit.setDelFlag("0");
		List<ProductUnit> productUnitList = productUnitService.findList(productUnit);
		int total = productUnitService.findTotal(productUnit);
		Page<ProductUnit> page = new Page<ProductUnit>(productUnit.getPage(), productUnit.getRows(), total);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("rows", productUnitList);
		dataMap.put("page", page);
		return dataMap;
	}

	/**
	 * 获取所有数据
	 * 
	 * @param modelMap
	 * @param productUnit
	 * @return
	 * @throws Exception
	 */
	// @RequiresPermissions("productUnit:productUnit:select")
	@RequestMapping(value = "/list/all", method = RequestMethod.POST)
	@ResponseBody
	public Object listAll(HttpSession session, HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductUnit productUnit) throws Exception {
	    productUnit.setDelFlag("0");
		List<ProductUnit> productUnitList = productUnitService.findAll(productUnit);
		return productUnitList;
	}

	/**
	 * 保存新增的数据
	 * 
	 * @param modelMap
	 * @param productUnit
	 * @return
	 * @throws Exception
	 */
	// @RequiresPermissions("productUnit:productUnit:insert")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public String save(HttpSession session, HttpServletResponse response, HttpServletRequest request, ModelMap modelMap,
			ProductUnit productUnit) throws Exception {
		if (!beanValidator(modelMap, productUnit)) {
			RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
			return null;
		}
		
		// 判断当前名称是否重复
        ProductUnit queryProductUnit = new ProductUnit();
        queryProductUnit.setUnitName(productUnit.getUnitName());
        queryProductUnit.setDelFlag("0");
        List<ProductUnit> productUnits = productUnitService.findList(queryProductUnit);
        if(null != productUnits && productUnits.size() > 0){// 如果存在则不能保存
            modelMap.put("message", "单位名称已经存在");
            RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
            return null;
        }
		
		productUnitService.save(productUnit);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}

	/**
	 * 保存修改的数据
	 * 
	 * @param modelMap
	 * @param productUnit
	 * @return
	 * @throws Exception
	 */
	// @RequiresPermissions("productUnit:productUnit:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public String update(HttpSession session, HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductUnit productUnit) throws Exception {
		if (!beanValidator(modelMap, productUnit)) {
			RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
			return null;
		}
		
		// 判断修改之后的名称是否重复
		ProductUnit queryProductUnit = new ProductUnit();
		queryProductUnit.setUnitName(productUnit.getUnitName());
		queryProductUnit.setDelFlag("0");
		List<ProductUnit> productUnits = productUnitService.findList(queryProductUnit);
		if(null != productUnits && productUnits.size() > 0){// 如果存在则不能保存
		    modelMap.put("message", "单位名称已经存在");
		    RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
		    return null;
		}
		
		productUnitService.update(productUnit);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}

	/**
	 * 删除数据库
	 * 
	 * @param modelMap
	 * @param productUnitId
	 * @return
	 * @throws Exception
	 */
	// @RequiresPermissions("productUnit:productUnit:delete")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete(HttpSession session, HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, String id) throws Exception {
		List<String> listId = Arrays.asList(id.split(","));
		productUnitService.delete(listId);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}

	/**
	 * 假删除
	 * 
	 * @param modelMap
	 * @param productUnit
	 * @return
	 * @throws Exception
	 */
	// @RequiresPermissions("productUnit:productUnit:delete")
	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	@ResponseBody
	public String remove(HttpSession session, HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductUnit productUnit) throws Exception {
		productUnitService.remove(productUnit);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}

	/**
	 * 导出Excel数据
	 * 
	 * @param modelMap
	 * @param productUnit
	 * @return
	 * @throws Exception
	 */
	// @RequiresPermissions("productUnit:productUnit:export")
	@RequestMapping(value = "/export")
	public String export(HttpSession session, HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductUnit productUnit) throws Exception {
		String fileName = DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		List<ProductUnit> productUnitList = productUnitService.findAll(productUnit);
		new ExportExcel(null, ProductUnit.class).setDataList(productUnitList).write(response, fileName).dispose();
		return null;
	}

	/**
	 * 添加商品单位
	 * 
	 * @param response
	 * @param request
	 * @return
	 */
	// 请求数据格式 {"unitName":"狗"}
	@RequestMapping(value = "/addProdUnit", method = RequestMethod.POST)
	@ResponseBody
	public Result addProdUnit(HttpServletResponse response, HttpServletRequest request) {
		String unitName = request.getParameter("unitName");

		if (unitName != null && !unitName.equals("")) {
			ProductUnit productUnit = new ProductUnit();
			productUnit.setUnitName(unitName);
			productUnitService.save(productUnit);
			return Result.success(null, "添加成功");

		} else {
			return Result.success("添加失敗");
		}
	}

	/**
	 * 删除一条 商品单位 数据
	 * 
	 * @param response
	 * @param request
	 * @return
	 */
	// 请求数据格式{"id":"0"}
	@RequestMapping(value = "/deleteProdUnit", method = RequestMethod.POST)
	@ResponseBody
	public Result deleteProdUnit(HttpServletResponse response, HttpServletRequest request) {
		String id = request.getParameter("id");

		if (id != null && !id.equals("")) {
			productUnitService.delete(Arrays.asList(id.split(",")));
			return Result.success(null, "删除成功");
		} else {
			return Result.failure("删除失败");
		}
	}

	/**
	 * 修改一条商品单位 数据
	 * 
	 * @param response
	 * @param request
	 * @return
	 */
	// 请求数据格式 {"prodUnitJson":{"id":"","unitName":""}}
	@RequestMapping(value = "/updateProdUnit", method = RequestMethod.POST)
	@ResponseBody
	public Result updateProdUnit(HttpServletResponse response, HttpServletRequest request) {
		String json = request.getParameter("prodUnitJson");

		if (json != null && !json.equals("")) {
			String id = (String) JSONPath.read(json, "$.id");
			ProductUnit prodUnit = productUnitService.findOne(id);
			String unitName = (String) JSONPath.read(json, "$.unitName");
			prodUnit.setUnitName(unitName);
			productUnitService.save(prodUnit);
			return Result.success(null, "修改成功");
		} else {
			return Result.failure("修改失败");
		}
	}

	/**
	 * 查詢所有商品單位 數據
	 * 
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findAllProdUnit", method = RequestMethod.POST)
	@ResponseBody
	public Result findAllProdUnit(HttpServletResponse response, HttpServletRequest request,ProductUnit pUnit) {
		pUnit.setDelFlag("0");
		List<ProductUnit> units=productUnitService.findAll(pUnit);
		if (units!=null&&units.size()>0) {
			return Result.success(units , "查询成功");
		}else {
			return Result.failure("查询失败");
		}
	}

}